//
//  LPNineTwentyNormalQuestionViewController.m
//  ShangHaiHuDongApp
//
//  Created by liuyingping on 2018/9/26.
//  Copyright © 2018年 刘应平. All rights reserved.
//

#import "LPNineTwentyNormalQuestionViewController.h"
#import "ZJQuestionAnTableViewCell.h"
#import "ZJRefurbish.h"
#import "ZJQADetailViewController.h"
#import "ZJQASharchView.h"
#import "LPWKWebViewController.h"
#define CELLID @"ZJQuestionAnTableViewCell"
@interface LPNineTwentyNormalQuestionViewController ()<UITableViewDelegate,UITableViewDataSource,ZJQASharchViewDelegate>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 刷新 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/** 搜索视图 */
@property (nonatomic, strong) ZJQASharchView *sharchView;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation LPNineTwentyNormalQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"常见问题";
    [self.view addSubview:self.sharchView];
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
    }];
    
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];;
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark -  网络请求
- (void)netWorking_header{
    
    NSString *urlString;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    NSString *searchString = self.sharchView.textField.text ? self.sharchView.textField.text : @"";
    paramer[@"SELECT"] = searchString;
    paramer[@"PAGESTART"] = @"1";
    paramer[@"PAGECOUNT"] = @"10";
    paramer[@"DEPT_ID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
    paramer[@"GLOBALID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.GLOBALID;
    if (searchString.length != 0) {
        paramer[@"SOLVE"] = @"2";
        urlString = [NetWorkingURL solrFindQAListForSolr];
    }else{
        paramer[@"SOLVE"] = @"";
        urlString = [NetWorkingURL qaActionQuestion];
    }

    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:urlString Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJQuestionAnModel class] json:[object objectForKey:@"QUESTION_MEG"]] mutableCopy];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataArray.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
            }
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"ERROR_MSG"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)netWorking_footer{
    NSString *urlString;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    NSString *searchString = self.sharchView.textField.text ? self.sharchView.textField.text : @"";
    paramer[@"SELECT"] = searchString;
    paramer[@"PAGESTART"] = [NSString stringWithFormat:@"%ld",self.page];
    paramer[@"PAGECOUNT"] = @"10";
    paramer[@"DEPT_ID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
    paramer[@"GLOBALID"] = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.GLOBALID;
    if (searchString.length != 0) {
        paramer[@"SOLVE"] = @"2";
        urlString = [NetWorkingURL solrFindQAListForSolr];
    }else{
        paramer[@"SOLVE"] = @"";
        urlString = [NetWorkingURL qaActionQuestion];
    }
  
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:urlString Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray * array = [NSArray yy_modelArrayWithClass:[ZJQuestionAnModel class] json:[object objectForKey:@"QUESTION_MEG"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 搜索
- (void)action_sharch:(UITextField *)textField{
    if (textField.text.length > 0) {
        _page = 1;
        [self netWorking_header];
    }
}

- (void)ZJQASharchView_delegate_search{
    NSLog(@"你点了搜索");
}

#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQuestionAnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.headLine = @"常见问题";
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQuestionAnModel *model = self.dataArray[indexPath.row];
    return model.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQuestionAnModel *model = self.dataArray[indexPath.row];
    /**  跳web  */
    /**  这里是跳web  */
    LPWKWebViewController *vc = [LPWKWebViewController new];
    vc.headLine = model.QUESTION;
    vc.urlString = model.URL;
    [self.navigationController pushViewController:vc animated:YES];
        
    
    
}

#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.sharchView.bottom, self.view.width, self.view.height - self.sharchView.bottom) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZJQuestionAnTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
        
    }
    return _refurbish;
}
- (ZJQASharchView *)sharchView{
    if (_sharchView == nil){
        _sharchView = [[ZJQASharchView alloc]initWithFrame:CGRectMake(0, LP_TopHeight , self.view.width, 44 * [UIScreen zj_screenHScale])];
        [_sharchView.textField addTarget:self action:@selector(action_sharch:) forControlEvents:UIControlEventAllEvents];
        _sharchView.delegate = self;
        
    }
    return _sharchView;
}

@end
